package com.ruoyi.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.UserProject;
import com.ruoyi.user.service.IUserProjectService;

import cn.hutool.core.collection.CollectionUtil;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.IUserSystemMsgService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 咨询师项目Controller
 * 
 * @author DEREK
 * @date 2023-06-15
 */
@RestController
@RequestMapping("/user/project")
public class UserProjectController extends BaseController
{
    @Autowired
    private IUserProjectService userProjectService;
    @Autowired
    private IUserSystemMsgService userSystemMsgService;

    /**
     * 查询咨询师项目列表
     */
    @PreAuthorize("@ss.hasPermi('user:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserProject userProject)
    {
        startPage();
        List<UserProject> list = userProjectService.selectUserProjectList(userProject);
        return getDataTable(list);
    }

    /**
     * 导出咨询师项目列表
     */
    @PreAuthorize("@ss.hasPermi('user:project:export')")
    @Log(title = "咨询师项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserProject userProject)
    {
        List<UserProject> list = userProjectService.selectUserProjectList(userProject);
        ExcelUtil<UserProject> util = new ExcelUtil<UserProject>(UserProject.class);
        util.exportExcel(response, list, "咨询师项目数据");
    }

    /**
     * 获取咨询师项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
    	UserProject byId = userProjectService.getById(id);
    	if(StringUtils.isNotEmpty(byId.getImg())) {
    		String[] split = byId.getImg().split(",");
    		byId.setSimpleSpectrum1(Arrays.asList(split));
    	}
        return AjaxResult.success(byId);
    }

    /**
     * 新增咨询师项目
     */
    @PreAuthorize("@ss.hasPermi('user:project:add')")
    @Log(title = "咨询师项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserProject userProject)
    {
        return toAjax(userProjectService.save(userProject));
    }

    /**
     * 修改咨询师项目
     */
    @PreAuthorize("@ss.hasPermi('user:project:edit')")
    @Log(title = "咨询师项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserProject userProject)
    {
    	if(CollectionUtil.isNotEmpty(userProject.getSimpleSpectrum1())) {
    		userProject.setImg(userProject.getSimpleSpectrum1().stream().map(String::valueOf).collect(Collectors.joining(",")));
    	}
    	boolean updateById = userProjectService.updateById(userProject);
    	if(2 == userProject.getStatus()) {
    		if(StringUtils.isEmpty(userProject.getRejected())) {
    			return AjaxResult.error("驳回理由不能为空");
    		}
    		userSystemMsgService.pushSystemMsg(userProject.getUserId(), "咨询项目审核驳回", "您发布的咨询项目审核驳回,理由如下:"+userProject.getRejected());
    	}else if(0 == userProject.getStatus()){
    		userSystemMsgService.pushSystemMsg(userProject.getUserId(), "咨询项目审核通过", "您发布的咨询项目审核已通过！");
    	}
        return toAjax(updateById);
    }

    /**
     * 删除咨询师项目
     */
    @PreAuthorize("@ss.hasPermi('user:project:remove')")
    @Log(title = "咨询师项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userProjectService.deleteUserProjectByIds(ids));
    }
}
